package com.example.rg1weatherandroidapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.rg1weatherandroidapp.image.SmartImageView;
import com.squareup.okhttp.Request;

public class MainActivity extends AppCompatActivity {

    private TextView textViewCity;
    private TextView textViewWeather;
    private TextView textViewTemp;
    private SmartImageView imageView;
    private RelativeLayout relativeLayout;
    private WeatherInfo weatherInfo;
    private int index=0;

    private GestureDetector gestureDetector;
    private TextView movictextView;

    /*final static String BASEURL="http://192.168.2.139:8080/RGWeatherWebService/WeatherServlet";
    final static String IMAGEURL="http://192.168.2.139:8080/drawable/";*/

    /*final static String BASEURL="http://sdchengjie.uicp.net/WeatherService/WeatherServlet";
    final static String IMAGEURL="http://sdchengjie.uicp.net/drawable/";*/

    final static String BASEURL="http://sdchengjie.uicp.net/MovieServer/MovieListServlet";
    final static String IMAGEURL="http://sdchengjie.uicp.net/drawable/";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        relativeLayout=findViewById(R.id.relativeLayout);
        imageView=findViewById(R.id.imageView);
        textViewCity=findViewById(R.id.textViewCity);
        textViewWeather=findViewById(R.id.textViewWeather);
        textViewTemp=findViewById(R.id.textViewTemp);

        movictextView=findViewById(R.id.movictextView);

        requestWeatherInfo(0);
        initWeather();
        gestureDetector=new GestureDetector(this,new GestureDetector.SimpleOnGestureListener(){
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                if((e2.getRawX()-e1.getRawX())>200)
                {
                    index++;
                    index=index%3;
                    requestWeatherInfo(index);
                    return true;
                }
                if((e1.getRawX()-e2.getRawX())>200)
                {
                    index--;
                    index=(index+3)%3;
                    requestWeatherInfo(index);
                    return true;
                }
                return super.onFling(e1, e2, velocityX, velocityY);
            }
        });


    }

    private void requestWeatherInfo(int index)
    {
        String city="郑州";
        switch (index)
        {
            case 0:city="郑州";
            break;
            case 1:city="上海";
                break;
            case 2:city="北京";
                break;
        }
        //使用OKHTTP发送异步请求
        //1.设置请求参数
        OkHttpClientManager.Param param1=new OkHttpClientManager.Param("City",city);
        //2.发送请求并获取请求响应数据
        OkHttpClientManager.postAsyn(BASEURL, new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {
                Log.e("error",e.getMessage());
            }

            @Override
            public void onResponse(String response) {
               //weatherInfo=response;
                movictextView.setText(response);
                initWeather();
            }
        }, param1);//（请求地址，返回结果，请求参数）


    }

    private void  initWeather()
    {
        if(weatherInfo!=null)
        {
            textViewCity.setText(weatherInfo.getCity());
            textViewWeather.setText(weatherInfo.getWeather());
            textViewTemp.setText(weatherInfo.getTemp()+"");
            OkHttpClientManager.setBackGround(relativeLayout,IMAGEURL+weatherInfo.getWeatherBg());
            imageView.setImageUrl(IMAGEURL+weatherInfo.getWeatherImg());
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
}
